package com.dfdchem.imd.kaiwu.adapter;

import android.annotation.SuppressLint;
import android.net.Uri;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import com.dfdchem.imd.kaiwu.R;
import com.dfdchem.imd.kaiwu.activity.base.BaseActivityUM;
import com.dfdchem.imd.kaiwu.manager.GlideManager;
import com.dfdchem.imd.kaiwu.model.SysMessage;
import com.dfdchem.imd.kaiwu.model.User;
import com.dfdchem.imd.kaiwu.util.RegexUtil;
import com.dfdchem.imd.kaiwu.util.RequestUtil;
import com.dfdchem.imd.kaiwu.util.StringUtil;
import com.dfdchem.imd.kaiwu.util.TextViewLinkHandler;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

public class SysMessageDetailAdapter extends RecyclerView.Adapter<SysMessageDetailAdapter.ViewHolder> {

    private final static String TAG = "SysMessageDetailAdapter";

    private AppCompatActivity context;

    private SimpleDateFormat sdf;

    @SuppressLint("SimpleDateFormat")
    public SysMessageDetailAdapter(AppCompatActivity context, User fromUser, User toUser) {
        this.context = context;
        this.fromUser = fromUser;
        this.toUser = toUser;
        sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    }

    private List<SysMessage> messages;
    private User fromUser;
    private User toUser;

    public List<SysMessage> getMessages() {
        return messages;
    }

    public void setMessages(List<SysMessage> messages) {
        this.messages = messages;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public ImageView avatar;
        public TextView time;
        public TextView content;

        public ViewHolder(View view) {
            super(view);
            avatar = view.findViewById(R.id.avatar);
            time = view.findViewById(R.id.time);
            content = view.findViewById(R.id.content);
        }
    }

    @Override
    public SysMessageDetailAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                                 int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_sys_message_detail, parent, false);

        return new ViewHolder(view);
    }

    @SuppressLint({"UseCompatLoadingForDrawables", "SetTextI18n"})
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        SysMessage message = messages.get(position);

        if (fromUser != null) {
            String avatar_url = fromUser.getAvatar();
            if (StringUtil.isEmpty(avatar_url)) {
                holder.avatar.setImageDrawable(context.getDrawable(R.drawable.ic_default_avatar));
            } else {
                // 适配内网地址
                if (avatar_url.startsWith(RequestUtil.url_file_short)) {
                    avatar_url = RequestUtil.URL_BASE_API + avatar_url;
                }

                new GlideManager(context).load(holder.avatar, avatar_url);
            }
        } else {
            holder.avatar.setImageDrawable(context.getDrawable(R.drawable.ic_default_avatar));
        }

        // RichText.fromHtml(message.getContent() + "  test").into(holder.content);

        holder.content.setText(Html.fromHtml(RegexUtil.contentClickable(message.getContent())));
        holder.content.setMovementMethod(new TextViewLinkHandler(){

            @Override
            public void onLinkClick(String url) {
                if (url == null || url.equals("")) {
                    return;
                }

                Uri uri = Uri.parse(url);

                if (context instanceof BaseActivityUM) {
                    ((BaseActivityUM) context).handleScheme(uri);
                }
            }
        });
        holder.time.setText(date2String(message.getSendTime()));
    }

    @Override
    public int getItemCount() {
        return messages == null ? 0 : messages.size();
    }

    private String date2String(Date date){
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);

        Calendar nowCalendar = new GregorianCalendar();
        nowCalendar.setTime(new Date());

        return sdf.format(date);
    }
}
